/**
* SVG library
*@author Duca Bogdan Mihai
*/
//global namespace
var SVG_NS="http://www.w3.org/2000/svg";
svg = {
	/*
	*Creates the svg node, and returns it
	*@param {number} w is width of the object
	*@param {number} h is height of the object
	*@return {node} svg
	*/
	createSVGNode : function (widthBox, heightBox, w, h) {
		var svg=document.createElementNS(SVG_NS,"svg");
		svg.setAttribute("viewBox","0 0 "+widthBox+" "+heightBox);
		/*svg.setAttribute("width",w);
		svg.setAttribute("height",h);*/
	/*	svg.width=w;
		svg.height=h;*/
		return svg;
	},
	
	/**
	*Creates the path node. Returns the node
	*@param {number} x of the initial point
	*@param {number} y of the initial point
	*@param {number} x2 of the finish point
	*@param {number} y2 of the finish poitn
	*@param {string} color for the path  stroke
	*@param {string} width of the line drawn by the path
	*@param {string} fill the path or not
	*@return path a path node element
	*/
	createPathNode:function(x,y,x2,y2,color,width,fill){
		var pathNode = document.createElementNS(SVG_NS,"path");
		pathNode.setAttribute("d","M "+x+" "+y+" L "+x2+" "+y2);
		pathNode.setAttribute("stroke",color);
		pathNode.setAttribute("stroke-width",width);
		pathNode.setAttribute("fill",fill);
		return pathNode;
	},
	/*createPathNode2:function(x,y,points,color,z,width,fill){
		var pathNode = document.createElementNS(SVG_NS,"path");
		for(var i=0; max=points.length; i<max;i+=1)
			{
			}
		pathNode.setAttribute("d","M"+x+" "+y+" L "+x2+" "+y2);
		pathNode.setAttribute("stroke",color);
		pathNode.setAttribute("stroke-width",width);
		pathNode.setAttribute("fill",fill);
		return pathNode;
	},*/
	/**
	*Function created for the node Text
	*@param x position x
	*@param y position y
	*@param name the content of the node
	*@param fontSize of the content
	*@return text object
	*/
	createTextNode:function(x,y,name,fontSize,color){
		var textNode = document.createElementNS(SVG_NS,"text");
		var textForNode = document.createTextNode(name);
		textNode.setAttribute("x",x);
		textNode.setAttribute("y",y);
		textNode.setAttribute("font-size",fontSize);
		textNode.setAttribute("fill",color);
		textNode.appendChild(textForNode);
		 return textNode;
		},
	/**
	*Function created for the node rect
	*@param x position x
	*@param y position y
	*@param width of the rectangle
	*@param height of the rectangle
	*@param color of the rectangle
	*@return rect object
	*/
	createRect : function (x,y,width,height,color) {
		var rectEl = document.createElementNS( SVG_NS,"rect");
		rectEl.setAttribute("x",x);
		rectEl.setAttribute("y",y);
		rectEl.setAttribute("width",width);
		rectEl.setAttribute("height",height);
		rectEl.setAttribute("color",(color)?color:"black");
		return rectEl;
		},
	/**
	*Function created for the node g, returns a simple g node, which is a container in SVG
	*@return svg node
	*/
	createGNode:function(){
		var gNode = document.createElementNS(SVG_NS,"g");
		return gNode;
		},
	/**
	*Function created to create an arrow with a body formed from a rectangle
	*@param x the starting x position
	*@param y the starting y position
	*@param rectangleWidth the width of the body of the arrow,in this case the rectangle
	*@param rectangleHeight the height of the body of the arrow,in this case the rectangle
	*@param arrowHeadWidth the width of the arrow head
	*@param arrowHeadHeight the height of the arrow head
	*@param rectangleColor the color of the body, in this case the rectangle
	*@param arrowHeadColor the color of the arrow Head
	*@return g node which is a container for the elements
	*/
	createArrowNode:function(x,y,rectangleWidth,rectangleHeight,arrowHeadWidth,arrowHeadHeight,rectangleColor,arrowHeadColor)
	{
		var gNode = document.createElementNS(SVG_NS,"g");
		var rectEl = document.createElementNS(SVG_NS,"rect");
		var pathNode = document.createElementNS(SVG_NS,"path");
		var xArrowHead = x+rectangleWidth;  //x of the arrow for path
		var yArrowHead = y;					//y of the arrow for the path
		//if(	(rectangleWidth<=0) || (rectangleHeight<=0) || (arrowHeadWidth<=0) || (arrowHeadHeight<=0))
		//verifies if the height of the arrow is different. if it is it makes the appropiate modifications for the new y
		if(rectangleHeight >= arrowHeadHeight){
			//if the arrow's height is smaller than the new y for the arrow head will be increased
			yArrowHead +=(rectangleHeight-arrowHeadHeight)/2;
			}
			else if(rectangleHeight <= arrowHeadHeight){
			yArrowHead -=(arrowHeadHeight-rectangleHeight)/2;
			}
		rectEl.setAttribute("x",x);
		rectEl.setAttribute("y",y);
		rectEl.setAttribute("width",rectangleWidth);
		rectEl.setAttribute("height",rectangleHeight);
		rectEl.setAttribute("fill",rectangleColor);
		gNode.appendChild(rectEl);
		//hier is drawn the arrow 
		pathNode.setAttribute("d","M"+(xArrowHead)+" "+yArrowHead+" L"+(xArrowHead+arrowHeadWidth)+" "+(yArrowHead+arrowHeadHeight/2)+" L"+(xArrowHead)+" "+ (yArrowHead+arrowHeadHeight)+" z");
		pathNode.setAttribute("stroke",arrowHeadColor);
		pathNode.setAttribute("fill",arrowHeadColor);
		gNode.appendChild(pathNode);
		
		return gNode;
	},
	/**
	*Createa a g node, for grouping 2 elements, one is a path and one is a text node.
	*
	*
	*@return g node
	*/
	createTextPathNode:function(xPath,yPath,xPath2,yPath2,colorPath, widthPath, fillPath, xText, yText,nameText,heighText,colorText){
		var gNode = document.createElementNS(SVG_NS,"g");
		var path = this.createPathNode(xPath,yPath,xPath2,yPath2,colorPath,widthPath,fillPath);
		var text = this.createTextNode(xText,yText,nameText,heighText,colorText);
		gNode.appendChild(text);
		gNode.appendChild(path);
		
		return gNode;
	}

};